<?php
/**
 * 单页
 */
namespace App\Service;

use App\Models\EwPage;

class PageService  extends BaseService
{
	const DISPLAY_TRUE = 1;		//显示标识：是
	const DISPLAY_FALSE = 0;	//显示标识：否
	const DEL_TRUE = 1;			//删除标识：是
	const DEL_FALSE = 0;		//删除标识：否

	/**
	 * [getList 获取列表]
	 * @param  [type] $params [description]
	 * @return [type]         [description]
	 */
	public static function getList($params, $count = FALSE)
	{
		$query = EwPage::query();
		$query->columns(['DISTINCT(id) AS id,content,is_display,is_del,create_time']);
		$query->andWhere("is_del = " . self::DEL_FALSE);

		//总条数
		if ($count)
			return $query->execute()->count();
		//order_by 排序
		if (isset($params->order_by)){
			$sql = parent::orderBy($params->order_by);
			$query->orderBy($sql);
		}

		//分页
		$query->limit($params->page_num,($params->p-1)*$params->page_num);

		return $query->execute();
	}

	/**
	 * [getInfo 通过主键获取单页]
	 * @param  integer $id [description]
	 * @return [type]      [description]
	 */
	public static function getInfo($id = 0)
	{
		return EwPage::findFirst($id);
	}

	/**
	 * [getPage 通过page_sn获取]
	 * @param  [type] $params [description]
	 * @return [type]         [description]
	 */
	public static function getPage($params)
	{
		return EwPage::findFirst(['columns'=>'title,page_sn,content','conditions'=>'page_sn = "' . $params->page_sn . '"']);
	}

	/**
	 * [count 获取单页数量]
	 * @return [type] [description]
	 */
	public static function count()
	{
		return EwPage::count();
	}

	/**
	 * [up 上架]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function up($id)
	{
		if (isset($id) && $id)
			$data = EwPage::findFirst($id);

		if ($data){
			$data->is_display = self::DISPLAY_TRUE;
			return $data->save();
		}

		return NULL;
	}

	/**
	 * [down 下架]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function down($id)
	{
		if (isset($id) && $id)
			$data = EwPage::findFirst($id);

		if ($data){
			$data->is_display = self::DISPLAY_FALSE;
			return $data->save();
		}

		return NULL;
	}

	/**
	 * [delete 移除数据]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function delete($id)
	{
		if (isset($id) && $id)
			$data = EwPage::findFirst($id);
		
		if ($data){
			if($data->is_config == 1)
				return NULL;

			$data->is_del = self::DEL_TRUE;
			return $data->save();
		}

		return NULL;		
	}

	/**
	 * [restore 恢复数据]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function restore($id)
	{
		if (isset($id) && $id)
			$data = EwPage::findFirst($id);
		
		if ($data){
			$data->is_del = self::DEL_FALSE;
			return $data->save();
		}

		return NULL;
	}

	/**
	 * [delete 彻底删除数据]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function forever($id)
	{
		if (isset($id) && $id)
			$row = EwPage::findFirst($id);

		if ($row){
			if($row->is_config == 1)
				return NULL;
			
			return $row->delete();
		}

		return NULL;
	}

	/**
	 * [save 保存数据]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public static function save($data)
	{
		$row = new EwPage;
		if (isset($data->id) && $data->id)
			$row = EwPage::findFirst($data->id);

		return $row->save((array)$data);
	}
}